package com.ddf.publics.controller;

import com.ddf.publics.entity.Bid;
import com.ddf.publics.entity.Due;
import com.ddf.publics.entity.UserCoupon;
import com.ddf.publics.service.UserCouponService;
import com.ddf.publics.service.UserOrderService;
import com.ddf.publics.entity.UserInfo;
import com.ddf.publics.service.*;
import com.ddf.publics.tool.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * Created by Zero on 2020-6-24 8:22
 * 继承KeyExpirationEventMessageListener创建redis过期事件的监听类
 */

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    public RedisKeyExpirationListener(RedisMessageListenerContainer container) { super(container); }

    @Autowired
    private UserCouponService userCouponService;

    @Autowired
    private BidService bidService;

    @Autowired
    private ZeroService zeroService;
    @Autowired
    private UserOrderService userOrderService;
    @Autowired
    private LooseBidService looseBidService;

    /**
     * 针对redis数据失效事件，进行数据处理
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String key=message.toString();//生效的key
        String keystr;
        String keyid;
        try {
            keystr=key.substring(0,key.indexOf(":"));
            keyid=key.substring(keystr.length()+1);
        }catch (Exception e){
            //Key不能截取return此方法
            return;
        }

        switch (keystr){
            case "coupon"://优惠券过期操作
                userCouponService.redisCoupon(keyid);
                break;
            case "order":
                userOrderService.redisOrder(keyid);
                break;
            case "bid"://定期自动回款
                bidService.dq(keyid);
                break;
            case "loan"://借贷逾期
                zeroService.LoanDue(keyid);
                break;
            case "looseBid"://散标
                looseBidService.repayment(keyid);
        }
    }
}